#!/bin/bash

# 设置数据库容器名和数据库信息
CONTAINER_NAME="my-mysql"
MYSQL_PASSWORD="root"
DATABASE_NAME="teacher_management_system"
SQL_FILE="/test1.sql"  # 文件路径，容器内的路径

# 1. 复制备份文件到 MySQL 容器
echo "复制备份文件到容器..."
docker cp test1.sql ${CONTAINER_NAME}:${SQL_FILE}

# 2. 进入容器并登录 MySQL
echo "进入 MySQL 容器并登录..."
docker exec -it ${CONTAINER_NAME} bash -c "
  mysql -u root -p${MYSQL_PASSWORD} -e \"CREATE DATABASE IF NOT EXISTS ${DATABASE_NAME};\"
"

# 3. 执行备份还原操作
echo "开始还原数据库 ${DATABASE_NAME}..."
docker exec -it ${CONTAINER_NAME} bash -c "
  mysql -u root -p${MYSQL_PASSWORD} ${DATABASE_NAME} < ${SQL_FILE}
"

# 4. 清理：删除容器内的备份文件
echo "删除容器内的备份文件..."
docker exec -it ${CONTAINER_NAME} rm -f ${SQL_FILE}

echo "数据库 ${DATABASE_NAME} 还原完成！"
